package com.hub.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hub.frame.consts.ConstantUtil;
import com.hub.frame.pojo.CachedUserInfo;
import com.hub.frame.service.cache.ICached;
import com.hub.utils.BaseEntity;
import com.hub.utils.date.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * BaseServiceImpl
 *
 * @description: 基础serviceImpl
 * @author hub
 * @create: 2020-05-18 17:43
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements IService<T> {

    @Resource
    private ICached cached;
    @Autowired
    private HttpServletRequest request;

    protected String getUserName(){
        String accessToken = request.getHeader(ConstantUtil.X_ACCESS_TOKEN);
        Object obj = cached.getObj(accessToken);
        if(obj == null){
            return null;
        }
        CachedUserInfo userInfo = (CachedUserInfo) obj;
        return userInfo.getUserName();
    }

    @Override
    public boolean save(T entity) {
        String userName = this.getUserName();
        entity.setCreateTime(DateUtils.getNow());
        entity.setCreateUser(userName);
        entity.setModifyTime(DateUtils.getNow());
        entity.setModifyUser(userName);
        entity.setActiveFlg(ConstantUtil.YES);
        return this.retBool(this.baseMapper.insert(entity));
    }

    @Override
    public boolean saveBatch(Collection<T> entityList, int batchSize){
        String userName = this.getUserName();
        entityList.forEach(entity -> {
            entity.setCreateTime(DateUtils.getNow());
            entity.setCreateUser(userName);
            entity.setModifyTime(DateUtils.getNow());
            entity.setModifyUser(userName);
            entity.setActiveFlg(ConstantUtil.YES);
        });
        return super.saveBatch(entityList, batchSize);
    }

    @Override
    public boolean updateById(T entity){
        String userName = this.getUserName();
        entity.setModifyTime(DateUtils.getNow());
        entity.setModifyUser(userName);
        entity.setActiveFlg(ConstantUtil.YES);
        return this.retBool(this.baseMapper.updateById(entity));
    }

    @Override
    public boolean update(T entity, Wrapper<T> updateWrapper) {
        String userName = this.getUserName();
        entity.setModifyTime(DateUtils.getNow());
        entity.setModifyUser(userName);
        entity.setActiveFlg(ConstantUtil.YES);
        return this.retBool(this.baseMapper.update(entity, updateWrapper));
    }

    @Override
    public boolean removeById(Serializable id){
        T entity = this.baseMapper.selectById(id);
        if(entity == null){
            return false;
        }
        String userName = this.getUserName();
        entity.setModifyTime(DateUtils.getNow());
        entity.setModifyUser(userName);
        entity.setActiveFlg(ConstantUtil.NO);
        return SqlHelper.delBool(this.baseMapper.updateById(entity));
    }

    @Override
    public boolean remove(Wrapper<T> wrapper){
        List<T> list = this.baseMapper.selectList(wrapper);
        if(list == null || list.size() == 0){
            return false;
        }
        T entity = Optional.ofNullable(list)
                .orElse(new ArrayList<>()).get(0);

        entity.setModifyTime(DateUtils.getNow());
        entity.setModifyUser(this.getUserName());
        entity.setActiveFlg(ConstantUtil.NO);
        return this.retBool(this.baseMapper.update(entity, wrapper));
    }

}
